package com.ruoyi.basic.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.domain.BasicAppoint;
import com.ruoyi.basic.domain.BasicPost;
import com.ruoyi.basic.domain.BasicUser;
import com.ruoyi.basic.mapper.BasicUserMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basic.mapper.BasicRewardMapper;
import com.ruoyi.basic.domain.BasicReward;
import com.ruoyi.basic.service.IBasicRewardService;
import com.ruoyi.common.core.text.Convert;

/**
 * 年度考核奖励Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-15
 */
@Service
public class BasicRewardServiceImpl extends ServiceImpl<BasicRewardMapper, BasicReward> implements IBasicRewardService
{

    @Autowired
    private BasicUserMapper userMapper;

    /**
     * 查询年度考核奖励
     * 
     * @param rewardId 年度考核奖励主键
     * @return 年度考核奖励
     */
    @Override
    public BasicReward selectBasicRewardByRewardId(Long rewardId)
    {
        return baseMapper.selectBasicRewardByRewardId(rewardId);
    }

    /**
     * 查询年度考核奖励
     *
     * @param rewardIds 年度考核奖励id
     * @return 年度考核奖励
     */
    @Override
    public List<BasicReward> selectBasicRewardByRewardIds(String rewardIds)
    {
        LambdaQueryWrapper<BasicReward> queryWrapper = new LambdaQueryWrapper<BasicReward>();
        queryWrapper.in(BasicReward::getRewardId, Convert.toLongArray(rewardIds));
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 查询年度考核奖励列表
     * 
     * @param basicReward 年度考核奖励
     * @return 年度考核奖励
     */
    @Override
    public List<BasicReward> selectBasicRewardList(BasicReward basicReward)
    {
        return baseMapper.selectBasicRewardList(basicReward);
    }

    /**
     * 新增年度考核奖励
     * 
     * @param basicReward 年度考核奖励
     * @return 结果
     */
    @Override
    public int insertBasicReward(BasicReward basicReward)
    {
        basicReward.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(basicReward);
    }

    /**
     * 修改年度考核奖励
     * 
     * @param basicReward 年度考核奖励
     * @return 结果
     */
    @Override
    public int updateBasicReward(BasicReward basicReward)
    {
        basicReward.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(basicReward);
    }

    /**
     * 批量删除年度考核奖励
     * 
     * @param rewardIds 需要删除的年度考核奖励主键
     * @return 结果
     */
    @Override
    public int deleteBasicRewardByRewardIds(String rewardIds)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(rewardIds)));
    }

    /**
     * 删除年度考核奖励信息
     * 
     * @param rewardId 年度考核奖励主键
     * @return 结果
     */
    @Override
    public int deleteBasicRewardByRewardId(Long rewardId)
    {
        return baseMapper.deleteById(rewardId);
    }

    /**
     * 导入年度考核奖励
     *
     * @param rewardList 年度考核奖励
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importReward(List<BasicReward> rewardList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(rewardList) || rewardList.size() == 0)
        {
            throw new ServiceException("导入年度考核奖励数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        int row = 2;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BasicReward reward : rewardList)
        {
            try
            {
                if(StringUtils.isEmpty(reward.getRewardName())){
                    continue;
                }
                //获取用户信息
                BasicUser user = userMapper.selectOne(new LambdaQueryWrapper<BasicUser>()
                        .eq(BasicUser::getIdCard, reward.getIdCard()).last("limit 1"));
                if(StringUtils.isNotNull(user)) {
                    reward.setUserId(user.getId());
                }else{
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、第 " + row + "行导入失败，员工 " + reward.getUserName() + " 不存在");
                    continue;
                }

                BasicReward info = baseMapper.selectOne(new LambdaQueryWrapper<BasicReward>()
                        .eq(BasicReward::getRewardName, reward.getRewardName())
                        .eq(BasicReward::getRewardYear, reward.getRewardYear())
                        .eq(BasicReward::getUserId, reward.getUserId()).last("limit 1"));
                if (StringUtils.isNull(info))
                {
                    reward.setCreateBy(operName);
                    this.insertBasicReward(reward);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、员工 " + reward.getUserName() + "年度考核奖励 " + reward.getRewardName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    reward.setRewardId(info.getRewardId());
                    reward.setUpdateBy(operName);
                    this.updateBasicReward(reward);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、员工 " + reward.getUserName() + "年度考核奖励 " + reward.getRewardName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、员工 " + reward.getUserName() + "年度考核奖励 " + reward.getRewardName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、员工 " + reward.getUserName() + " 年度考核奖励导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
            finally {
                row++;
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
